// 提示框
function modelShow(
	title = '提示',
	content = '确认执行此操作吗?',
	callback = () => {},
	showCancel = true,
	cancelText = '取消',
	confirmText = '确定'
) {
	uni.showModal({
		title: title,
		content: content,
		showCancel: showCancel,
		cancelText: cancelText,
		confirmText: confirmText,
		cancelText: cancelText,
		success: function(res) {
			if (res.confirm) {
				// 用户点击确定操作
				setTimeout(() => {
					callback()
				}, 500)
			} else if (res.cancel) {
				// 用户取消操作
			}
		}
	})
};


//货币格式化
function formatMoney(number, places, symbol, thousand, decimal) {
	number = number || 0
	places = !isNaN((places = Math.abs(places))) ? places : 2
	symbol = symbol !== undefined ? symbol : '￥'
	thousand = thousand || ','
	decimal = decimal || '.'
	var negative = number < 0 ? '-' : '',
		i = parseInt((number = Math.abs(+number || 0).toFixed(places)), 10) + '',
		j = (j = i.length) > 3 ? j % 3 : 0
	return (
		symbol +
		negative +
		(j ? i.substr(0, j) + thousand : '') +
		i.substr(j).replace(/(\d{3})(?=\d)/g, '$1' + thousand) +
		(places ?
			decimal +
			Math.abs(number - i)
			.toFixed(places)
			.slice(2) :
			'')
	)
}

/**
 * 金额相加
 * @param {Object} value1
 * @param {Object} value2
 */
function moneySum(value1, value2) {
	return (parseFloat(value1) + parseFloat(value2)).toFixed(2);
}
/**
 * 金额相减
 * @param {Object} value1
 * @param {Object} value2
 */
function moneySub(value1, value2) {
	let res = (parseFloat(value1) - parseFloat(value2)).toFixed(2);
	return res > 0 ? res : 0;
}

/**
 * 时间格式
 * @param {Object} time
 * @param {Object} type(ymd，ymd-hms，hm)
 */
function longDateFormate(time, type) {
	if (!time) return '';

	const timestamp = new Date(time).getTime();
	if (timestamp < 0) {
		return ''
	}

	time = time.replace(/T/g, ' '); //去掉T
	time = time.replace(/-/g, '/');
	time = time.replace(/\.\d+/, ' '); //去掉毫秒

	const timeFlag = new Date(time);

	let y = timeFlag.getFullYear();
	let m = numFormat(timeFlag.getMonth() + 1);
	let d = numFormat(timeFlag.getDate());
	let h = numFormat(timeFlag.getHours());
	let min = numFormat(timeFlag.getMinutes());
	let s = numFormat(timeFlag.getSeconds());
	if (type == 'ymd') {
		return `${y}-${m}-${d}`;
	}
	if (type == 'ymd-hms') {
		return `${y}-${m}-${d} ${h}:${min}:${s}`;
	}
	if (type == 'hm') {
		return `${h}:${min}`;
	}

	return `${y}-${m}-${d} ${h}:${min}`;
}

/**
 * string转int
 * @param {Object} d 
 */
const numFormat = (d = '') => {
	const data = d * 1;
	return data < 10 ? '0' + data : data;
};

/**
 * 根据id获取返回的label
 * @param {Object} id
 * @param {Object} list
 */
function getLabelFromID(id, list = []) {
	const info = list.find(item => {
		return id == item.value;
	});
	if (info) {
		return info.label;
	}
	return '';
}

function getTextFromID(id = '', list = [], name = 'text') {
	const info = list.find(item => {
		return id == item.id;
	});
	if (info) {
		return list.find(item => {
			return id == item.id;
		})[name];
	}
	return '';
};

//一月中的第几周
const getMonthWeekCur = (a, b, c) => {
	let date = new Date(a, parseInt(b) - 1, c),
		w = date.getDay(),
		d = date.getDate();
	return Math.ceil(
		(d + 6 - w) / 7
	);
};



//获取当前月有几周
const getWeekNum = (year, month) => {
	let d = new Date()
	d.setFullYear(year, month, 0)
	let monthDays = d.getDate() // 该月天数
	let weeks = 0 // 该月周数
	// 计算第一周
	d.setFullYear(year, month - 1, 1) // 该月第一天
	let w1 = d.getDay() // 获取星期几(0~6) 该月第一天 星期几
	// 按照星期一到星期天的形式计算
	if (w1 === 0) {
		// 如果这个月的一号是这周的最后一天
		// weeks = 1
		weeks = 0
		monthDays = monthDays - 1
	} else {
		// 如果这个月的一号不是这周的最后一天
		weeks = 1
		monthDays = monthDays - (7 - w1 + 1)
	}
	// 计算最后一周天数
	d.setFullYear(year, month, 0)
	const newmonthDays = d.getDate()
	d.setFullYear(year, month - 1, newmonthDays) // 该月第最后一天
	const w2 = d.getDay() // 获取星期几(0~6) 该月第一天 星期几
	if (w2 === 0) {
		monthDays = monthDays - 7
	} else {
		monthDays = monthDays - w2
	}
	weeks = weeks + 1 // 再加最后一周
	weeks = weeks + (monthDays / 7) // 加上剩余的周数
	return weeks
}

//一年中的第几周
const getYearWeekCur = (y, m, d) => {
	var targetDay = new Date(y, m - 1, d);
	var year = targetDay.getFullYear();
	var month = targetDay.getMonth();
	var days = targetDay.getDate();
	//那一天是那一年中的第多少天
	for (var i = 1; i < m; i++) {
		days += getMonthDays(year, i);
	}
	//那一年第一天是星期几
	var yearFirstDay = new Date(year, 0, 1).getDay();
	//计算是第几周
	days += yearFirstDay;
	var week = Math.ceil(days / 7);
	return week;
}

//判断年份是否为润年
const isLeapYear = (year) => {
	return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}

//获取某年某月的天数
const getMonthDays = (year, month) => {
	return [31, (isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month - 1];
}

const getWeekDate = (year, month, weekday) => {
	year = year * 1;
	month = month * 1;
	let list = [];
	let d = new Date();
	d.setFullYear(year, month - 1, 1);
	const w1 = d.getDay();
	d.setDate(d.getDate() - w1);
	//周日~周一为一周
	// const start = d.setDate(d.getDate() + 7*(weekday-1));
	// const end = d.setDate(d.getDate() + 6);
	const start = d.setDate(d.getDate() + 7 * (weekday - 1) + 1);
	const end = d.setDate(d.getDate() + 6);
	return {
		start: start,
		end: end
	}
}

// 格式化
const dateFormat = (time, type = 'ymd-hm') => {
	if (time) {
		const timestamp = new Date(time).getTime();
		if (timestamp < 0) {
			return ''
		}
		let date = new Date(timestamp);
		let y = date.getFullYear();
		let m = numFormat(date.getMonth() + 1);
		let d = numFormat(date.getDate());
		let h = numFormat(date.getHours());
		let min = numFormat(date.getMinutes());
		let s = numFormat(date.getSeconds());
		if (type == 'ymd') {
			return `${y}-${m}-${d}`;
		} else if (type == 'ymd-hms') {
			return `${y}-${m}-${d} ${h}:${min}:${s}`;
		} else if (type == 'hm') {
			return `${h}:${min}`;
		} else {
			return `${y}-${m}-${d} ${h}:${min}`;
		}
	}
	return '';
};



export {
	formatMoney,
	modelShow,
	moneySum,
	moneySub,
	longDateFormate,
	getLabelFromID,
	getTextFromID,
	getWeekNum,
	getYearWeekCur,
	getWeekDate,
	dateFormat,
	getMonthWeekCur,
	numFormat
}